package cn.fl.insurance.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.insurance.condition.InsTypCfgRQueryCondition;
import cn.fl.insurance.constant.InsuranceConstant;
import cn.fl.insurance.dto.InsTypCfgRDTO;
import cn.fl.insurance.service.IInsPlcBscInfoApiService;
import cn.fl.insurance.service.IInsTypCfgRApiService;
import cn.fl.insurance.service.IInsTypCfgRService;
import cn.fl.insurance.service.IInsTypInfoApiService;
import cn.fl.insurance.transform.InsTypCfgRVOTransform;
import cn.fl.insurance.vo.InsTypCfgRVO;
import cn.fl.project.service.IProjectApiService;

/**
 * @Description: 险种配置关系表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class InsTypCfgRServiceImpl implements IInsTypCfgRService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(InsTypCfgRServiceImpl.class);

    /**
     * TODO 自定义 API 暴露服务
     */
    @Autowired
    private IInsTypCfgRApiService insTypCfgRApiService;
    /**
     * 组织架构API
     */
    @Autowired
    private ISysOrgApiService sysOrgApiService;
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private IInsPlcBscInfoApiService insPlcBscInfoApiService;
    @Autowired
    private IInsTypInfoApiService insTypInfoApiService;
    @Autowired
    private IProjectApiService projectApiService;

    @Override
    public InsTypCfgRVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        InsTypCfgRDTO dto = this.insTypCfgRApiService.selectInsTypCfgRById(id);
        return InsTypCfgRVOTransform.toVO(dto);
    }

    @Override
    public boolean add(InsTypCfgRVO entity) throws BaseException {
        if (null == entity) return false;
        return this.insTypCfgRApiService.addInsTypCfgR(InsTypCfgRVOTransform.toDTO(entity));
    }

    /**
     * 付款申请前验证是否需购买保险
     *
     * @param queryCondition
     * @return
     */
    public DataResultVO validate(InsTypCfgRQueryCondition queryCondition) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "验证通过");
        queryCondition.setIsFstReq(InsuranceConstant.CODE_IS_FST_REQ_YES);
        queryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        queryCondition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        DataResultDTO resultDTO=this.insTypCfgRApiService.validateIns(queryCondition);
        if("0".equals(resultDTO.getCode())){//验证不通过
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(resultDTO.getInfo());
        }
        return result;
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.insTypCfgRApiService.removeInsTypCfgRByIds(ids, CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(InsTypCfgRVO entity) throws BaseException {
        if (null == entity) return false;
        return this.insTypCfgRApiService.modifyInsTypCfgR(InsTypCfgRVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        page = this.insTypCfgRApiService.searchInsTypCfgRListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<InsTypCfgRDTO> dtoList = (List<InsTypCfgRDTO>) page.getResultList();
            this.settingBuOrgCdNm(dtoList);
            page.setResultList(InsTypCfgRVOTransform.toVOList(dtoList));
        }
        return page;
    }

    @Override
    public List<InsTypCfgRVO> searchList(InsTypCfgRQueryCondition condition) throws BaseException {
        List<InsTypCfgRDTO> dtoList = this.insTypCfgRApiService.searchInsTypCfgRList(condition);
        this.settingBuOrgCdNm(dtoList);
        return InsTypCfgRVOTransform.toVOList(dtoList);
    }

    /**
     * 查询组织架构 只查第二级 事业部
     *
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchBusinessOrgList() throws BaseException {
        List<SysOrgDTO> list = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getCurrentSysCd());
        List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
        if (CollectionUtils.isEmpty(list)) {
            return kvList;
        }
        for (SysOrgDTO sysOrgDTO : list) {
            if (sysOrgDTO.getOrgLvl() == 2L) {
                KeyValueVO kv = new KeyValueVO();
                kv.setKey(sysOrgDTO.getOrgCd());
                kv.setValue(sysOrgDTO.getOrgNm());
                kvList.add(kv);
            }
        }
        return kvList;
    }

    /**
     * 查询项目类型KV
     *
     * @return
     * @throws BaseException
     */
    public List<KeyValueVO> getPrjTypList(String buOrgCd) throws BaseException {
        List<KeyValueVO> kvList = this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_PRJ_TYPE_CD, CurrentThreadContext.getCurrentSysCd(), buOrgCd);
        return kvList;
    }

    /**
     * 设置组织架构名称
     *
     * @param dtoList
     */
    public void settingBuOrgCdNm(List<InsTypCfgRDTO> dtoList) {
        List<KeyValueVO> list = this.searchBusinessOrgList();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        for (KeyValueVO vo : list) {
            map.put(vo.getKey(), vo.getValue());
        }

        for (InsTypCfgRDTO dto : dtoList) {
            if (map.containsKey(dto.getBuOrgCd())) {
                dto.setBuOrgCdNm(map.get(dto.getBuOrgCd()));
            }
        }
    }
}
